package com.demo;
 
    /**
    * Created with IntelliJ IDEA.
    * User: victor
    * Date: 13-10-11
    * Time: 下午3:09
    * To change this template use File | Settings | File Templates.
    */
 
    import java.io.File;
 
    import java.io.FileInputStream;
 
    import java.security.KeyStore;
    import java.security.cert.CertificateException;
    import java.security.cert.X509Certificate;

    import javax.net.ssl.SSLContext;

    import org.apache.hc.client5.http.classic.methods.HttpGet;
    import org.apache.hc.client5.http.impl.classic.BasicHttpClientResponseHandler;
    import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
    import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
    import org.apache.hc.client5.http.impl.classic.HttpClients;
    import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManagerBuilder;
    import org.apache.hc.client5.http.io.HttpClientConnectionManager;
    import org.apache.hc.client5.http.protocol.HttpClientContext;
    import org.apache.hc.client5.http.ssl.SSLConnectionSocketFactory;
    import org.apache.hc.client5.http.ssl.SSLConnectionSocketFactoryBuilder;
    import org.apache.hc.client5.http.ssl.TrustAllStrategy;
    import org.apache.hc.core5.http.ClassicHttpRequest;
    import org.apache.hc.core5.http.HttpHost;
    import org.apache.hc.core5.http.io.HttpClientResponseHandler;
    import org.apache.hc.core5.http.io.entity.EntityUtils;
    import org.apache.hc.core5.http.protocol.HttpContext;
    import org.apache.hc.core5.ssl.SSLContexts;
//    import org.apache.http.HttpEntity;
//
//    import org.apache.http.client.methods.CloseableHttpResponse;
//
//    import org.apache.http.client.methods.HttpGet;
//
//    import org.apache.http.conn.ssl.SSLContexts;
//
//    import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
//
//    import org.apache.http.impl.client.CloseableHttpClient;
//
//    import org.apache.http.impl.client.HttpClients;
//
//    import org.apache.http.util.EntityUtils;
 
 
 
    /**
    * 代码展示了如果使用ssl context创建安全socket连接
    */
 
    public class ClientCustomSSL {
 
        public final static void main(String[] args) throws Exception {
 
//            KeyStore trustStore  = KeyStore.getInstance(KeyStore.getDefaultType());
//
//            //加载证书文件
//
//            FileInputStream instream = new FileInputStream(new File("/home/victor/my.store"));
//
//            try {
//
//                trustStore.load(instream, "mypassword".toCharArray());
//
//            } finally {
//
//                instream.close();
//
//            }
//            SSLContext sslcontext = SSLContexts.custom().loadTrustMaterial(trustStore).build();
//
//            SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext,
//
//                    SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
//
//            CloseableHttpClient httpclient = HttpClients.custom()
//
//                    .setSSLSocketFactory(sslsf)
//
//                    .build();
//
//            try
//
//            {
//
//                //访问支付宝
//
//                HttpGet httpget = new HttpGet("https://www.alipay.com/");
//
//                System.out.println("executing request" + httpget.getRequestLine());
//
//                CloseableHttpResponse response = httpclient.execute(httpget);
//
//                try {
//
//                    HttpEntity entity = response.getEntity();
//
//                    System.out.println("----------------------------------------");
//
//                    System.out.println(response.getStatusLine());
//
//                    if (entity != null) {
//
//                        System.out.println(EntityUtils.toString(entity));
//
//                    }
//
//                } finally {
//
//                    response.close();
//
//                }
//
//            } finally {
//
//                httpclient.close();
//
//            }
//


            SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null,new TrustAllStrategy(){
                // 信任所有
                public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                    return true;
                }
            }).build();

            SSLConnectionSocketFactory sslSocketFactory = SSLConnectionSocketFactoryBuilder.create().setSslContext(sslContext).build();

            HttpClientConnectionManager cm = PoolingHttpClientConnectionManagerBuilder.create()
                    .setSSLSocketFactory(sslSocketFactory)
                    .build();
            CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(cm)
                    .build();

//            HttpGet httpGet = new HttpGet("https://www.baidu.com");
//            HttpClientContext clientContext = HttpClientContext.create();
            HttpClientResponseHandler<?> respHandler = new BasicHttpClientResponseHandler();
            HttpHost request = HttpHost.create("https://www.baidu.com");
            ClassicHttpRequest clientContext = new HttpGet("https://www.baidu.com") ;
            Object execute = httpClient.execute(request, clientContext, respHandler);
//            CloseableHttpResponse response = httpClient.execute(httpGet, clientContext);

            System.out.println(execute);
            System.out.println("----------------------------------------");
//            System.out.println(response.getCode() + " " + response.getReasonPhrase());
//            System.out.println(EntityUtils.toString(response.getEntity()));

        }
        
 
    } 